package com.hcloud.gateway.config;

import com.hcloud.gateway.webhandler.HystrixFallbackHandler;
import com.hcloud.gateway.webhandler.ImageCodeWebHandler;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;

/**
 * @author hepangui
 * @date 2018/11/5
 * 路由配置信息
 * 特殊请求直接在此处理，不进行路由转发
 */
@Slf4j
@Configuration
@AllArgsConstructor
public class RouterFunctionConfiguration {
    /**
     * 不使用AutoWird，因为可以直接用构造器注入，使用了@AllArgsConstructor注解
     */
	private final HystrixFallbackHandler hystrixFallbackHandler;
	private final ImageCodeWebHandler imageCodeWebHandler;

	@Bean
	public RouterFunction routerFunction() {
		return RouterFunctions.route(
			RequestPredicates.path("/fallback")
				.and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), hystrixFallbackHandler)
			.andRoute(RequestPredicates.GET("/code")
				.and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), imageCodeWebHandler);

	}

}
